﻿using System;
using System.Windows.Forms;
using Oxage.Midi;

namespace Oxage.Forms
{
	public partial class UdpForm : UserControl, IConfigPanel
	{
		#region Constructor
		public UdpForm()
		{
			InitializeComponent();
		}
		#endregion

		#region Properties
		public bool IsLocked
		{
			set
			{
				txtHost.Enabled = !value;
				txtPort.Enabled = !value;
			}
		}

		public string Hostname
		{
			get
			{
				return txtHost.Text;
			}
			set
			{
				txtHost.Text = value;
			}
		}

		public int Port
		{
			get
			{
				int result = 0;
				int.TryParse(txtPort.Text, out result);
				if (result <= 0)
				{
					result = 6434;
				}
				txtPort.Text = result.ToString();
				return result;
			}
			set
			{
				txtPort.Text = value.ToString();
			}
		}
		#endregion

		#region IConfigPanel Members
		public void SetProfile(Profile profile)
		{
			if (profile != null)
			{
				this.Hostname = (!string.IsNullOrEmpty(profile["Hostname"]) ? profile["Hostname"] : "");

				int port = 0;
				int.TryParse(profile["Port"], out port);
				this.Port = (port > 0 ? port : 6434);
			}
		}

		public Profile GetProfile()
		{
			Profile profile = new Profile();
			profile["Hostname"] = this.Hostname;
			profile["Port"] = this.Port.ToString();
			return profile;
		}

		public UserControl GetUserControl()
		{
			return this;
		}
		#endregion
	}
}
